<template>
  <div class="app-container app-js df fdc">
    <el-form class="top-form" :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="phoneNumber">
        <el-input v-model="queryParams.phoneNumber" placeholder="请输入手机号" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="回复状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择回复状态" clearable style="width: 200px">
          <el-option v-for="dict in sys_feedback_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <div class="f1">
      <el-table height="100%" v-loading="loading" :data="feedbackList" @selection-change="handleSelectionChange">
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column label="序号" align="center" type="index" width="60" />
        <el-table-column label="姓名" align="center" prop="name" width="120" />
        <el-table-column label="手机号" align="center" prop="phoneNumber" width="120" />
        <el-table-column label="留言内容" align="center" prop="content" :show-overflow-tooltip="true" />
        <el-table-column label="回复内容" align="center" prop="remark" :show-overflow-tooltip="true" />
        <el-table-column label="留言时间" align="center" prop="createTime" width="160" />
        <el-table-column label="回复时间" align="center" prop="updateTime" width="160" />
        <el-table-column label="操作" align="center" class-name="small-padding fix-width">
          <template #default="scope">
            <el-button link type="primary" icon="View" @click="handleView(scope.row)" v-hasPermi="['system:feedback:query']">
              查看
            </el-button>
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:feedback:edit']">
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
      :selection="ids.length"
    />

    <!-- 查看留言对话框 -->
    <el-dialog title="查看留言" v-model="viewOpen" draggable :close-on-click-modal="false" width="600px" append-to-body>
      <el-descriptions :column="1" border>
        <el-descriptions-item label="姓名">{{ form.name }}</el-descriptions-item>
        <el-descriptions-item label="手机号">{{ form.phoneNumber }}</el-descriptions-item>
        <el-descriptions-item label="留言内容">{{ form.content }}</el-descriptions-item>
        <el-descriptions-item label="留言时间">{{ parseTime(form.createTime) }}</el-descriptions-item>
        <el-descriptions-item label="回复内容">{{ form.remark }}</el-descriptions-item>
        <el-descriptions-item label="回复时间">{{ parseTime(form.updateTime) }}</el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="viewOpen = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 编辑留言备注对话框 -->
    <el-dialog title="编辑留言备注" v-model="open" draggable :close-on-click-modal="false" width="600px" append-to-body>
      <el-form ref="feedbackRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="form.name" disabled />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="form.phoneNumber" disabled />
        </el-form-item>
        <el-form-item label="留言内容">
          <el-input v-model="form.content" type="textarea" :rows="3" disabled />
        </el-form-item>
        <el-form-item label="回复内容" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="3" placeholder="请输入备注信息" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Feedback">
import { listFeedback, getFeedback, updateFeedbackRemark } from '@/api/system/feedback'

const { proxy } = getCurrentInstance()

const { sys_feedback_status } = proxy.useDict('sys_feedback_status')

const feedbackList = ref([])
const open = ref(false)
const viewOpen = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 15,
    name: undefined,
    phoneNumber: undefined,
    status: undefined
  },
  rules: {
    remark: [{ required: false, message: '备注不能为空', trigger: 'blur' }]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询留言列表 */
function getList() {
  loading.value = true
  listFeedback(queryParams.value).then((response) => {
    feedbackList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}

/** 表单重置 */
function reset() {
  form.value = {
    feedbackId: undefined,
    name: undefined,
    phoneNumber: undefined,
    content: undefined,
    remark: undefined
  }
  proxy.resetForm('feedbackRef')
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.feedbackId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 查看按钮操作 */
function handleView(row) {
  reset()
  const feedbackId = row.feedbackId || ids.value
  getFeedback(feedbackId).then((response) => {
    form.value = response.data
    viewOpen.value = true
  })
}

/** 编辑按钮操作 */
function handleUpdate(row) {
  reset()
  const feedbackId = row.feedbackId || ids.value
  getFeedback(feedbackId).then((response) => {
    form.value = response.data
    open.value = true
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs['feedbackRef'].validate((valid) => {
    if (valid) {
      updateFeedbackRemark(form.value).then((response) => {
        proxy.$modal.msgSuccess('修改成功')
        open.value = false
        getList()
      })
    }
  })
}

getList()
</script>
